<template>
  <div v-loading="loading" class="dashboard-container">
    <div class="app-container">
      <el-card class="tree-card">
        <!-- 组织架构头部布局   抽离到tree-tools组件-->
        <!-- 此处:is-root="true" 父传子 也可以简写成  is-root -->
        <tree-tools :tree-node="company" is-root @addDepts="addDepts" />
        <!-- <el-row type="flex" justify="space-between" align="middle" style="height: 40px;width: 100%">
          <el-col>传智教育</el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <el-col>负责人</el-col>
              <el-col>
                <el-dropdown>
                  <span class="el-dropdown-link">
                    操作<i class="el-icon-arrow-down el-icon--right" />
                  </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item>添加子部门</el-dropdown-item>
                    <el-dropdown-item>编辑部门</el-dropdown-item>
                    <el-dropdown-item>删除部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row> -->
        <!-- 树形控件渲染 -->
        <!-- 这里的props和我们之前学习的父传子 的props没关系 -->
        <el-tree :data="departs" :props="defaultProps" default-expand-all>
          <!-- data当前节点的数据 -->
          <tree-tools
            slot-scope="{data}"
            :tree-node="data"
            @delDepts="getDepartment"
            @addDepts="addDepts"
            @editDepts="editDepts"
          />
          <!-- <el-row slot-scope="{data}" type="flex" justify="space-between" align="middle" style="height: 40px;width: 100%">
            <el-col>{{ data.name }}</el-col>
            <el-col :span="4">
              <el-row type="flex" justify="end">
                <el-col>负责人</el-col>
                <el-col>
                  <el-dropdown @command="clickFn">
                    <span class="el-dropdown-link">
                      操作<i class="el-icon-arrow-down el-icon--right" />
                    </span>
                    <el-dropdown-menu slot="dropdown">
                      <el-dropdown-item :command="data">添加子部门</el-dropdown-item>
                      <el-dropdown-item>编辑部门</el-dropdown-item>
                      <el-dropdown-item>删除部门</el-dropdown-item>
                    </el-dropdown-menu>
                  </el-dropdown>
                </el-col>
              </el-row>
            </el-col>
          </el-row> -->
        </el-tree>
      </el-card>
    </div>
    <!-- 弹框 抽离成组件 -->
    <addDepts
      ref="addDepts"
      :visible.sync="dialogVisible"
      :tree-node="node"
      @addAdepts="getDepartment"
    />
    <!-- <el-dialog
      title="提示"
      :visible.sync="dialogVisible"
      width="30%"
      :before-close="handleClose"
    >
      <span>这是一段信息</span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="dialogVisible = false">确 定</el-button>
      </span>
    </el-dialog> -->
  </div>
</template>

<script>
import treeTools from '@/views/departments/components/tree-tools'
import { getDepartmentList } from '@/api/departments'
import { tranListToTreeData } from '@/utils/index'
import addDepts from '@/views/departments/components/add-depts'

export default {
  name: 'VueAdminTemplateMasterIndex',
  components: { treeTools, addDepts },

  data() {
    return {
      dialogVisible: false,
      company: {},
      departs: [],
      // 注意：树形控件，默认读取label属性作为节点名称，读取children属性来渲染子节点
      // 可以通过在标签上添加props属性来修改节点名称和渲染子节点的属性读取
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      node: {},
      loading: false
    }
  },

  mounted() {
  },
  created() {
    this.getDepartment()
  },

  methods: {
    // 编辑部门
    editDepts(treeNode) {
      this.dialogVisible = true
      this.node = treeNode
      this.$refs.addDepts.getDepartmentDetail(treeNode.id)
    },
    // 处理弹框关闭逻辑

    // 新增部门
    addDepts(treeNode) {
      this.dialogVisible = true
      this.node = treeNode
    },
    // 获取部门列表数据
    async getDepartment() {
      this.loading = true
      const res = await getDepartmentList()
      // console.log('res', res)
      this.departs = tranListToTreeData(res.depts, '')
      // 头部id和下面一级结构的pid相同
      this.company = { name: res.companyName, manager: '负责人', id: '' }
      this.loading = false
    }
  }
}
</script>

<style lang="scss" scoped>
.tree-card {
  padding: 30px  140px;
  font-size:14px;
}
</style>
